package gameObjects;

import gameObjects.Bike.POINTTO;

import java.nio.FloatBuffer;
import arena.Arena;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.shape.Box;
import com.jme.scene.state.MaterialState;
import com.jme.util.geom.BufferUtils;

public class Trail extends Node{
	private static final long serialVersionUID = 1904101532;
	private ColorRGBA 		color;
	private Box		  		trail;
	private Vector3f		startPoint;
	private Vector3f		endPoint;
	private Material    	ms;
	private FloatBuffer 	colBuffer;
	
	private float offSet;
	private float wallHeight;
	
	public Trail(String name, ColorRGBA color)
	{			
		colBuffer = BufferUtils.createColorBuffer(4);
		colBuffer.put(color.r).put(color.g).put(color.b).put(color.a);
		this.color=color;
		startPoint=new Vector3f(0, 0, 0);
		endPoint=new Vector3f(startPoint.clone());
		offSet=0.01f;
		wallHeight=0.3f;
		newTrailPart();
	}
	
	public void setColorBuffer(ColorRGBA color)
	{
		this.color=color;
		this.colBuffer = BufferUtils.createColorBuffer(4);
		this.colBuffer.put(color.r).put(color.g).put(color.b).put(color.a);
	}
	
	public ColorRGBA getColor() {
		return color;
	}

	public void setColor(ColorRGBA color) {
		this.color = color;
	}

	public Vector3f getStartPoint() {
		return startPoint;
	}

	public void setStartPoint(Vector3f startPoint) {
		this.startPoint = startPoint;
	}

	public Vector3f getEndPoint() {
		return endPoint;
	}

	public void setEndPoint(Vector3f endPoint) {
		this.endPoint = endPoint;
	}

	public void newTrailPart(Vector3f startPoint, Vector3f endPoint)
	{
		//System.out.println("TRAIL (newTrail) startpoint.x: " + startPoint.x );
		//System.out.println("TRAIL (newTrail)   endpoint.x: " +   endPoint.x );
		//System.out.println("TRAIL (newTrail) startpoint.z: " + startPoint.z );
		//System.out.println("TRAIL (newTrail)   endpoint.z: " +   endPoint.z );
		this.startPoint=startPoint;	
		this.endPoint=endPoint;			   
		newTrailPart();
	}
	
	private void newTrailPart()
	{
		this.attachChild(trail);
		this.updateRenderState();
		trail=new Box("TRAILPART",startPoint,endPoint);
		trail.setCullHint(CullHint.Dynamic);
		this.setMaterial();
		trail.updateRenderState();
		this.attachChild(trail);
		this.updateRenderState();
	}
	
	public void updateTrailPart(Vector3f endPoint,POINTTO pointTo)
	{
		Vector3f start = null;
		Vector3f end   = null;
		switch(pointTo)
		{
		case NORTH:{
			start = new Vector3f(startPoint.x-offSet, 0.0f, startPoint.z);
			end   = new Vector3f(endPoint.x  +offSet, wallHeight, endPoint.z);
			break;
		}
		case SOUTH:{
			start = new Vector3f(startPoint.x+offSet, 0.0f, startPoint.z);
			end   = new Vector3f(endPoint.x  -offSet, wallHeight, endPoint.z);
			break;
		}
		case EAST:{
			start = new Vector3f(startPoint.x, 0.0f, startPoint.z+offSet);
			end   = new Vector3f(endPoint.x  , wallHeight, endPoint.z-offSet);
			break;
		}
		case WEST:{
			start = new Vector3f(startPoint.x, 0.0f, startPoint.z-offSet);
			end   = new Vector3f(endPoint.x  , wallHeight, endPoint.z+offSet);
			break;
		}
		}
		trail.updateGeometry(start, end);
		trail.updateRenderState();
		this.updateRenderState();
	}
	
	public Spatial getTrail()
	{
		return this;
	}
	
	public void setMaterial()
	{
		ms = new Material();
		ms.setMaterial(Arena.colorAmb);
		trail.setRenderState(ms.getMaterial());
	}
	
	public MaterialState getMaterial()
	{
		return ms.getMaterial();
	}
	
}
